package com.ara.common.kernel.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.ara.common.kernel.R;
import com.ara.common.kernel.listener.OnActivityListener;
import com.gyf.immersionbar.ImmersionBar;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


/**
 * Created by XieXin on 2017-09-05.
 * 基础Activity
 */
public abstract class BaseActivity<VB extends ViewBinding> extends AppCompatActivity implements View.OnClickListener {
    public ActivityListener activityListener = new ActivityListener();
    protected VB binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).statusBarColor(R.color.white).fitsSystemWindows(true).statusBarDarkFont(true).init();
        binding = getViewBinding();
        setContentView(binding.getRoot());
        initView(savedInstanceState);
        initListener();
        initData();
    }

    private VB getViewBinding() {
        try {
            //获取当前类的Type
            Type type = getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType) {//判断是否支持泛型
                //获取泛型中的实例
                Class<VB> clazz = (Class<VB>) ((ParameterizedType) type).getActualTypeArguments()[0];
                //反射VB里面的inflate方法
                Method method = clazz.getMethod("inflate", LayoutInflater.class);
                //调用方法获得VB
                return (VB) method.invoke(null, getLayoutInflater());
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public abstract void initView(@Nullable Bundle savedInstanceState);

    public void initData() {
    }

    public void initListener() {
    }

    @Override
    public void onClick(View view) {
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected Context getContext() {
        return this;
    }

    protected Activity getActivity() {
        return this;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (activityListener.onActivityListener != null)
            activityListener.onActivityListener.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (activityListener.onActivityListener != null)
            activityListener.onActivityListener.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    /**
     * 重写 getResource 方法，防止系统字体影响
     */
    @Override
    public Resources getResources() {//禁止app字体大小跟随系统字体大小调节
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1.0f) {
            android.content.res.Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1.0f;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }

    public static class ActivityListener {
        public OnActivityListener onActivityListener;
    }

}
